<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function numberToChinese(num) {
      var chineseNum = "";
      var chineseUnits = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
      var chinesePlace = ["", "十", "百", "千", "万", "亿"];

      var numStr = num.toString();
      var numLen = numStr.length;

      for (var i = 0; i < numLen; i++) {
        var digit = parseInt(numStr.charAt(i));
        var idx = numLen - i - 1;
        var unit = chinesePlace[idx % 6];

        if (digit === 0) {
          if (idx % 4 === 0 && idx > 0) {
            chineseNum = chineseNum.slice(0, -1);
            chineseNum += chinesePlace[4];
          }
          if (chineseNum.slice(-1) !== "零") {
            chineseNum += "零";
          }
        } else {
          chineseNum += chineseUnits[digit] + unit;
        }
      }

      return chineseNum;
    }

    console.log(numberToChinese(124));
    console.log(numberToChinese(24));
    console.log(numberToChinese(17044));
    console.log(numberToChinese(99999));
    console.log(numberToChinese(100412));

  </script>
</body>

</html>